/*
 * User: ophir
 * Date: Sep 9, 2008
 * Time: 11:08:02 PM
 */
package org.conann.common;

import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.collect.Maps;

import java.util.concurrent.ConcurrentMap;

public abstract class ConcurrentStaticCache<K, V> {

    private final ConcurrentMap<K, V> cache = Maps.newConcurrentHashMap();

    public V get(K key) {
        checkNotNull(key);
        V value = cache.get(key);
        if (value == null) {
            cache.putIfAbsent(key, create(key));
            value = cache.get(key);
        }
        return value;
    }

    public abstract V create(K key);
}